import pickle
import base64


class CookieSecret(object):
    # 加密
    @classmethod
    def dumps(cls, data):
        # 1.将数据装换成bytes
        data_bytes = pickle.dumps(data)
        # 2.将bytes使用base64序列化加密
        base64_bytes = base64.b64encode(data_bytes)
        # 3.将加密完毕的bytes以字符串类型输出
        base64_str = base64_bytes.decode()
        return base64_str

    # 解密
    @classmethod
    def loads(cls, data):
        # 1.将数据解密转成bytes
        base64_bytes = base64.b64decode(data)
        # 2.将bytes转回原来的python类型
        pickle_data = pickle.loads(base64_bytes)
        return pickle_data



# if __name__ == '__main__':
#     data_dict = {
#         1: {'count': 2, 'selecte': True},
#         2: {'count': 3, 'selecte': True}
#     }

    # 1.dict====>Str
    # json_str = json.dumps(data_dict)
    # # 2.str====>dict
    # json_dict = json.loads(json_str)
    #
    # # pickle: dict===>bytes
    # pickle_bytes = pickle.dumps(data_dict)
    #
    # # pickle:bytes===>dict
    # pickle_dict = pickle.loads(pickle_bytes)
    #
    # # 1.加密==>bytes
    # dumps_base64_bytes = base64.b64encode(pickle_bytes)
    #
    # # 2.解密
    # loads_base64 = base64.b64decode(dumps_base64_bytes)
    #
    # print(loads_base64)
    # print(type(loads_base64))
